492B - Vanya and Lanterns - CodeForces Solution


binary search implementation math sortings *1200

Please click on ads to support us..

Python Code:

def result(n):
    n=str(n)
    m=n.find(".")
    if m==-1:
        n+="."
        m=0
    for i in range(m,10):
        n+="0"
    return n
def count(a ,arr):
    n=0
    for i in range(len(arr)):
        if arr[i]==a:
            n+=1
        if arr[i]>a:
            return n
    return n
inp=input()
inp=inp.split(" ")
n=int(inp[0])
l=int(inp[1])
inp2=input()
inp2=inp2.split(" ")
inp2=[int(x) for x in inp2]
inp2.sort()
max=0
i_f=0
i_l=0
if inp2[0]>l-inp2[n-1]:
    max=inp2[0]*2
else:
    max=(l-inp2[n-1])*2
for i in range(n-1):
    
    if int(inp2[i+1])-int(inp2[i])>max:
        max=int(inp2[i+1])-int(inp2[i])
        i_f=inp2[i]
        i_l=inp2[i+1]
if n==1:
    if inp2[0]==0 or inp2[0]==l:
        max=l*2
    else:
        if inp2[0]>l-inp2[0]:
            max=inp2[0]*2
        else:
            max=(l-inp2[0])*2
    i_f=inp2[0]
    i_l=inp2[0]
if l-int(inp2[n-1])>max/2 and n!=1:
    if n==2:
        if int(inp2[1])-int(inp2[0])>0:
            max=(l-int(inp2[n-1]))*2
m=(count(i_f,inp2))
k=count(i_l,inp2)

print(result(max/(2)))

C++ Code:

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

#define PB push_back
#define INF (int)1e9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define ll long long
#define F first
#define S second
#define All(x) x.begin(), x.end()
#define Sort(x) sort(All(x))

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll n, l;
	cin>>n>>l;
	vector<double> a(n);
	for(ll i=0; i<n; i++)
		cin>>a[i];
	Sort(a);
	double md=0;
	for(ll i=0; i<n-1; i++)
		md=max(md, a[i+1]-a[i]);
	double x=0, y=0;
	if(a[0]!=0)
		x=a[0]-0;
	if(a[n-1]!=l)
		y=l-a[n-1];
	cout<<fixed << setprecision(9)<<max(md/2, max(x, y))<<endl;
	return 0;
}


Comments

Submit
0 Comments
More Questions

1370A - Maximum GCD
149A - Business trip
34A - Reconnaissance 2
59A - Word
462B - Appleman and Card Game
1560C - Infinity Table
1605C - Dominant Character
1399A - Remove Smallest
208A - Dubstep
1581A - CQXYM Count Permutations
337A - Puzzles
495A - Digital Counter
796A - Buying A House
67A - Partial Teacher
116A - Tram
1472B - Fair Division
1281C - Cut and Paste
141A - Amusing Joke
112A - Petya and Strings
677A - Vanya and Fence
1621A - Stable Arrangement of Rooks
472A - Design Tutorial Learn from Math
1368A - C+=
450A - Jzzhu and Children
546A - Soldier and Bananas
32B - Borze
1651B - Prove Him Wrong
381A - Sereja and Dima
41A - Translation
1559A - Mocha and Math